package org.dong.Dao;

import org.dong.Jdbc.JdbcUtil;
import org.dong.Model.Level;
import org.dong.Model.User;
import org.dong.Tool.Data;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class UserDaoImpl extends BeasDao implements UserDao {

    @Override
    public boolean checkLogin(Map<String,String> map) {

        try {
            String sql = "select * from dong_user where account = ? and password = ?";
            List<Object> list = new ArrayList<>();
            list.add(map.get("username"));
            list.add(map.get("password"));
            List<Object> user = super.select(sql,list,User.class);


           //如果存在用户的话
            if(user.size() > 0){

                Data.user =  (User)user.get(0);

                //查找用户可以使用的空间总量
                sql = "Select size from dong_user_level where id = ? ";
                List<Object> level = new ArrayList<>();
               level.add(Data.user.getLevel());
               BigDecimal level_cloud_size =  super.selectOne(sql,level, BigDecimal.class);
               //修改已经使用的空间值
                Data.user.setLevel_cloud_size(level_cloud_size);

                //统计已使用的空间大小
                sql = "select sum(size) from dong_file_list where user_id= ?";
                List<Object> sum = new ArrayList<>();
                sum.add(Data.user.getId());
                BigDecimal cloud_size = super.selectOne(sql,sum,BigDecimal.class);

                Data.user.setCloud_size(cloud_size);

                //更新已使用的空间
                sql = "update dong_user SET cloud_size=? where id = ? ";
                List<Object> update = new ArrayList<>();

                update.add(cloud_size);
                update.add(Data.user.getId());
                if(super.update(sql,update) <= 0){
                    return false;
                }


                return true;
            }else{
                return false;
            }

        } catch (SQLException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return false;
    }

    @Override
    public boolean checkAccount(Map<String,String> map) {


        try {
            String sql = "select * from dong_user where account = ? ";

            List<Object> list = new ArrayList<>();
            list.add(map.get("account"));

            if(super.select(sql,list,User.class).size() > 0){
                //如果存在的话就生成用户
                return  true;
            }else{
                return false;
            }

        } catch (SQLException | InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }

        return false;
    }

    @Override
    public boolean reg(Map<String,String> map) {

        try {
            String sql = "INSERT INTO dong_user( `username`, `account`, `password`, `phone`,  `level`, `cloud_size`) VALUES (?,?,?,?,1,0)";

            List<Object> list = new ArrayList<>();
            list.add(map.get("username"));
            list.add(map.get("account"));
            list.add(map.get("password"));
            list.add(map.get("phone"));


            if(super.update(sql,list) > 0){
                return true;
            }else{
                return  false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }

        return false;
    }
}
